﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Utils;
using Log;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Media;
using System.Drawing.Drawing2D;

namespace AutoTrader
{
    public partial class AutoTraderForm : Form
    {
        class OrderInfo
        {
            public int ID;
            public int Lots;
            public decimal Price;
            public decimal Taxes;
            public decimal TopBorder;
            public decimal BottomBorder;
            public string BuyOrSell;
            public OrderInfo()
            {
                ID = 0;
                Lots = 1;
                Price = 0;
                Taxes = 0;
                BuyOrSell = "B";
            }
        }

        private bool ReadOrderSettings()
        {
            try
            {
                if (!int.TryParse(Settings.Settings.BaseOrderID, out BaseOrder.ID))
                {
                    myLogger.Add(string.Format("Invalid data for BaseOrderID - {0}", Settings.Settings.BaseOrderID));
                    return false;
                }
                if (!int.TryParse(Settings.Settings.BaseLots, out BaseOrder.Lots))
                {
                    myLogger.Add(string.Format("Invalid data for BaseLots - {0}", Settings.Settings.BaseLots));
                    return false;
                }
                if (!int.TryParse(Settings.Settings.BaseBuyOrSell, out Buy0Sell1))
                {
                    myLogger.Add(string.Format("Invalid data for BaseBuy0Sell1 - {0}", Settings.Settings.BaseBuyOrSell));
                    return false;
                }
                if (!Decimal.TryParse(Settings.Settings.BasePrice, out BaseOrder.Price))
                {
                    myLogger.Add(string.Format("Invalid data for BasePrice - {0}", Settings.Settings.BasePrice));
                    return false;
                }
                if (Buy0Sell1 == 0)
                    BaseOrder.BuyOrSell = "0";
                else
                    BaseOrder.BuyOrSell = "1";

                if (!int.TryParse(Settings.Settings.StopOrderID, out StopOrder.ID))
                {
                    myLogger.Add(string.Format("Invalid data for StopOrderID - {0}", Settings.Settings.StopOrderID));
                    return false;
                }
                if (!int.TryParse(Settings.Settings.StopLots, out StopOrder.Lots))
                {
                    myLogger.Add(string.Format("Invalid data for StopLots - {0}", Settings.Settings.StopLots));
                    return false;
                }
                if (!int.TryParse(Settings.Settings.StopBuyOrSell, out Buy0Sell1))
                {
                    myLogger.Add(string.Format("Invalid data for StopBuySell - {0}", Settings.Settings.StopBuyOrSell));
                    return false;
                }
                if( !Decimal.TryParse( Settings.Settings.StopPrice, out StopOrder.Price ) )
                {
                    myLogger.Add( string.Format( "Invalid data for StopPrice - {0}", Settings.Settings.StopPrice ) );
                    return false;
                }
                if( Buy0Sell1 == 0 )
                    StopOrder.BuyOrSell = "0";
                else
                    StopOrder.BuyOrSell = "1";

                if( !Int32.TryParse( Settings.Settings.FailedCount, out FailedCount ) )
                {
                    myLogger.Add( string.Format( "Invalid data for FailedCount - {0}", Settings.Settings.FailedCount ) );
                    return false;
                }

                if (!Decimal.TryParse(Settings.Settings.TopBorder, out BaseOrder.TopBorder))
                {
                    myLogger.Add(string.Format("Invalid data for TopBorder - {0}", Settings.Settings.TopBorder));
                    return false;
                }

                if (!Decimal.TryParse(Settings.Settings.BottomBorder, out BaseOrder.BottomBorder))
                {
                    myLogger.Add(string.Format("Invalid data for BottomBorder - {0}", Settings.Settings.BottomBorder ) );
                    return false;
                }

                if (!Decimal.TryParse(Settings.Settings.TopBorder, out BaseOrder.TopBorder))
                {
                    myLogger.Add(string.Format("Invalid data for TopBorder - {0}", Settings.Settings.TopBorder));
                    return false;
                }
            }
            catch (Exception ex)
            {
                myLogger.Add( string.Format( "ReadOrderSettings : Runtime exception: {0}-{1}", ex.Message, ex.StackTrace));
                return false;
            }

            return true;
        }

        private bool WriteOrderSettings()
        {
            try
            {
                Settings.Settings.BaseOrderID = BaseOrder.ID.ToString();
                Settings.Settings.BaseLots = BaseOrder.Lots.ToString();
                Settings.Settings.BasePrice = BaseOrder.Price.ToString();
                Settings.Settings.BaseBuyOrSell = BaseOrder.BuyOrSell;

                Settings.Settings.StopOrderID = StopOrder.ID.ToString();
                Settings.Settings.StopLots = StopOrder.Lots.ToString();
                Settings.Settings.StopPrice = StopOrder.Price.ToString();
                Settings.Settings.StopBuyOrSell = StopOrder.BuyOrSell;
                Settings.Settings.FailedCount = FailedCount.ToString();
                Settings.Settings.Save();
            }
            catch( Exception ex )
            {
                myLogger.Add(string.Format("WriteOrderSettings : Runtime exception: {0}-{1}", ex.Message, ex.StackTrace));
                return false;
            }
            return true;
        }
        private bool GetSaveSettings()
        {
            try
            {
                if (!int.TryParse(Settings.Settings.ClientNumber, out ClientNumber ) )
                {
                    myLogger.Add(string.Format("Invalid data for ClientNumber - {0}", Settings.Settings.ClientNumber));
                    return false;
                }

                if (!int.TryParse(Settings.Settings.InitialLots, out InitialLots ) )
                {
                    myLogger.Add(string.Format("Invalid data for InitialLots - {0}", Settings.Settings.InitialLots));
                    return false;
                }

                Settings.Settings.Instrument = cbInstruments.SelectedItem.ToString();
                Settings.Settings.InitialLots = cbInitialLots.SelectedItem.ToString();
                int selectedIndex = cbMaxDouble.SelectedIndex;
                Settings.Settings.MaxDouble = cbMaxDouble.SelectedItem.ToString();
                Settings.Settings.PipsStep = cbPipsStep.SelectedItem.ToString();
                Settings.Settings.ClientNumber = textClientNumber.Text;

                if (!Decimal.TryParse(Settings.Settings.PipsStep, out PipsStep))
                {
                    myLogger.Add(string.Format("Invalid data for PipsStep - {0}", Settings.Settings.PipsStep));
                    return false;
                }

                PipsStep = Convert.ToDecimal( Settings.Settings.PipsStep ) * 0.0001m;

                if (chAutoStart.Checked)
                    Settings.Settings.AutoStart = "True";
                else
                    Settings.Settings.AutoStart = "False";

                if (chSmartStep.Checked)
                    Settings.Settings.SmartStep = "True";
                else
                    Settings.Settings.SmartStep = "False";

                Settings.Settings.Save();
            }
            catch (Exception ex)
            {
                myLogger.Add(string.Format("GetSaveSettings : Runtime exception: {0}-{1}", ex.Message, ex.StackTrace));
                return false;
            }
            return true;
        }

        private bool ReadSetSettings()
        {
            try
            {
                Settings.Settings.Read("");
                cbInstruments.SelectedItem = (object)Settings.Settings.Instrument;
                cbInitialLots.SelectedItem = (object)Settings.Settings.InitialLots;
                cbMaxDouble.SelectedItem = (object)Settings.Settings.MaxDouble;
                cbPipsStep.SelectedItem = (object)Settings.Settings.PipsStep;
                textClientNumber.Text = Settings.Settings.ClientNumber;
                if (Settings.Settings.AutoStart == "True")
                    chAutoStart.Checked = true;
                else
                    chAutoStart.Checked = false;
                if (Settings.Settings.SmartStep == "True")
                    chSmartStep.Checked = true;
                else
                    chSmartStep.Checked = false;
            }
            catch (Exception ex)
            {
                myLogger.Add(string.Format("ReadSetSettings : Runtime exception: {0}-{1}", ex.Message, ex.StackTrace ) );
                return false;
            }
            return true;
        }

        private bool ReadCoursesSettings()
        {
            try
            {
                if( !Decimal.TryParse(Settings.Settings.BuyPrice, out BuyPrice ) )
                {
                    myLogger.Add( string.Format("Invalid data for BuyPrice - {0}", Settings.Settings.BuyPrice ) );
                    return false;
                }
                if( !Decimal.TryParse( Settings.Settings.SellPrice, out SellPrice ) )
                {
                    myLogger.Add(string.Format("Invalid data for SellPrice - {0}", Settings.Settings.SellPrice ) );
                    return false;
                }
            }
            catch (Exception ex)
            {
                myLogger.Add(string.Format("ReadCoursesSettings : Runtime exception: {0}-{1}", ex.Message, ex.StackTrace));
                return false;
            }
            return true;
        }

        private bool WriteCoursesSettings()
        {
            try
            {
                Settings.Settings.SellPrice = SellPrice.ToString();
                Settings.Settings.BuyPrice = BuyPrice.ToString();
                Settings.Settings.Save();
            }
            catch (Exception ex)
            {
                myLogger.Add(string.Format("ReadCoursesSettings : Runtime exception: {0}-{1}", ex.Message, ex.StackTrace));
                return false;
            }
            return true;
        }
    }
}